package com.ccys.common.vo;

import com.ccys.common.enums.CodeEnum;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;

import static com.ccys.common.enums.CodeEnum.SUCCESSFUL;


/**
 * ResponseResult
 * <p>
 * Create By cary
 * 2018-07-12 13:41
 **/
@Data
@SuppressWarnings("unchecked")
public class ResponseResult<T, K> implements Serializable {
    private static final long serialVersionUID = 5935352669960485205L;
    @JsonProperty("code")
    private int code;
    private String msg;
    private T data;
    private K otherData;

    public ResponseResult() {
        this.code = SUCCESSFUL.codeOf();
        this.msg = SUCCESSFUL.msgOf();
        this.data = null;
        this.otherData = null;
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(int code, String msg, T data, K otherData) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.otherData = otherData;
    }

    private static final ResponseResult EMPTY_SUCCESS = of(SUCCESSFUL.codeOf(), SUCCESSFUL.msgOf(), null, null);
    private static final ResponseResult EMPTY_ERROR = of(CodeEnum.ERROR.codeOf(), CodeEnum.ERROR.msgOf(), null, null);
    private static final ResponseResult ERR_UNDEFINED = of(CodeEnum.UNDEFINED.codeOf(), CodeEnum.UNDEFINED.msgOf(), null, null);

    private static <T, K> ResponseResult of(Integer code, String msg, T data, K otherData) {
        return new ResponseResult(code, msg, data, otherData);
    }

    public static ResponseResult success() {
        return EMPTY_SUCCESS;
    }

    public static ResponseResult success(Integer code, String message) {
        return of(code, message, null, null);
    }

    public static <T> ResponseResult success(String message, T data) {
        return of(SUCCESSFUL.codeOf(), message, data, null);
    }

    public static <T> ResponseResult success(T data) {
        return of(SUCCESSFUL.codeOf(), SUCCESSFUL.msgOf(), data, null);
    }

    public static <T, K> ResponseResult success(T data, K otherData) {
        return of(SUCCESSFUL.codeOf(), SUCCESSFUL.msgOf(), data, otherData);
    }

    public static <T, K> ResponseResult success(String message, T data, K otherData) {
        return of(SUCCESSFUL.codeOf(), message, data, otherData);
    }

    public static ResponseResult undefined() {
        return ERR_UNDEFINED;
    }

    public static ResponseResult error(CodeEnum code) {
        return of(code.codeOf(), null, code.msgOf(), null);
    }

    public static ResponseResult error(Integer code, String message) {
        return of(code, message, null, null);
    }

    public static ResponseResult error(String message) {
        return of(CodeEnum.FAIL.codeOf(), message, null, null);
    }

    public static ResponseResult error() {
        return of(CodeEnum.FAIL.codeOf(), "参数未传", null, null);
    }

    public static ResponseResult update(Integer i) {
        if (i < 1) {
            return error(400, "操作失败,请刷新后重试");
        }
        return success(200, "操作成功");
    }

    public static <T> ResponseResult update(Integer i, T successData) {
        if (i < 1) {
            return error(400, "操作失败,请刷新后重试");
        }
        return success(successData);
    }
}
